home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Interfaces & Libraries / graphics libraries / storage library.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  6.1 KB  |  205 lines  |  [TEXT/MPS ]

  1. /* graphics libraries:  
  2.    primitive flattening library routines
  3.    by Cary Clark, Georgiann Delaney, Michael Fairman, Dave Good, Robert Johnson, Keith McGreggor, Oliver Steele, David Van Brink, Chris Yerga
  4.    Copyright 1987 - 1991 Apple Computer, Inc.  All rights reserved.  */
  5.  
  6.  
  7.    #include <Memory.h>
  8.    #include <Errors.h>
  9.    #include <Files.h>
  10. #include "font routines.h"
  11. #include "graphics libraries.h"
  12. #include "scaler types.h"
  13. #include "storage library.h"
  14. /*
  15.    The following routine is an example of how to write a spooling procedure to
  16.    work with GXFlattenShape and GXUnflattenShape.  For writing, it stores the data in
  17.    a Handle which must be disposed of after the GXFlattenShape call.  When reading,
  18.    it expects the 'data' field of the gxSpoolBlock to have a handle to the data to be
  19.    unflattened.
  20. */
  21.  
  22. #define allocationIncrement   1024     /* the storage handle is grown by this amount */
  23.  
  24. long HandleSpoolProc(gxSpoolCommand command,  userSpool *block)
  25. {
  26.    switch (command)
  27.    {
  28.       case gxOpenReadSpool:
  29.          block->size = 0;
  30.          block->position = 0;
  31.       break;
  32.       case gxOpenWriteSpool:
  33.          block->data = NewHandle(allocationIncrement);
  34.          block->size = allocationIncrement;
  35.          block->position = 0;
  36.       break;
  37.       
  38.       case gxReadSpool:
  39.          BlockMove((*(char **) block->data) + block->position, block->spool.buffer, block->spool.count);
  40.          block->position += block->spool.count;
  41.       break;
  42.  
  43.       case gxWriteSpool:
  44.       {  register long oldPosition;
  45.  
  46.          oldPosition = block->position;
  47.          block->position += block->spool.count;
  48.  
  49.          /* make sure there is at least enough room for one buffer size past current pointer */
  50.          if (block->position + block->spool.bufferSize > block->size)      
  51.          {
  52.             block->size += block->spool.bufferSize;
  53.             HUnlock(block->data);
  54.             SetHandleSize(block->data, block->size);
  55.             HLock(block->data);
  56.          }
  57.          BlockMove(block->spool.buffer, (*(char **) block->data + oldPosition), block->spool.count);
  58.       }
  59.       break;
  60.       
  61.       case gxCloseSpool:
  62.          SetHandleSize(block->data, block->position);
  63.       break;
  64.    }
  65.    return 0;
  66. }
  67.  
  68.  
  69. /*
  70.    This routine spools to and from a file.  The file must have previously been created and opened,
  71.    and its refNum is expected to be in the refnum field of the block.
  72. */
  73. static long FileSpoolProc(gxSpoolCommand command, userSpool *block)
  74. {
  75.    short err;
  76.    
  77.    switch (command) {
  78.       case gxOpenReadSpool:
  79.       break;
  80.       case gxOpenWriteSpool:
  81.       break;
  82.       case gxReadSpool:
  83.       {
  84.          long count = block->spool.count;
  85.          err = FSRead(block->reference, &count, block->spool.buffer);
  86.          IfDebug(err && err != eofErr, "\pFSRead failed");
  87.       } break;
  88.       case gxWriteSpool:
  89.       {
  90.          long count = block->spool.count;
  91.          err = FSWrite(block->reference, &count, block->spool.buffer);
  92.          IfDebug(err, "\pFSWrite failed");
  93.       } break;
  94.       case gxCloseSpool:
  95.       break;
  96.       default:
  97.          IfDebug(true, "\punexpected spool command");
  98.    }
  99.    return 0;
  100. }
  101.  
  102. Handle ShapeToHandle(gxShape source)
  103. {
  104.    userSpool block;
  105.    
  106.    NilShapeReturnNil(source);
  107.    block.spool.spoolProcedure = (long (*)(gxSpoolCommand, struct gxSpoolBlock *)) HandleSpoolProc;
  108.    block.spool.buffer = nil;
  109.    block.spool.bufferSize = 0;
  110.    GXFlattenShape(source, gxFontListFlatten | gxFontGlyphsFlatten, &block.spool);
  111.    return block.data;
  112. }
  113.  
  114. gxShape HandleToShape(Handle source, long count, const gxViewPort portList[])
  115. {
  116.    userSpool block;
  117.    
  118.    block.spool.spoolProcedure = (long (*)(gxSpoolCommand, struct gxSpoolBlock *)) HandleSpoolProc;
  119.    block.spool.buffer = nil;
  120.    block.spool.bufferSize = 0;
  121.    block.data = source;
  122.    return GXUnflattenShape(&block.spool, count, portList);
  123. }
  124.  
  125. void ShapeToFile(gxShape source, Str255 fileName, short vRefNum, OSType creator, OSType fileType)
  126. {
  127.    userSpool block;
  128.    short refNum;
  129.    short err;
  130.    
  131.    NilShapeReturn(source);
  132.    NilParamReturn(fileName);
  133.    
  134.    if (creator == 0) creator = 'sLib';
  135.    if (fileType == 0) fileType = 'flat';
  136.    err = Create(fileName, vRefNum, creator, fileType);
  137.    if (err == dupFNErr) {
  138.       FSDelete(fileName, vRefNum);
  139.       err = Create(fileName, vRefNum, creator, fileType);
  140.    }
  141.    IfDebug(err, "\pCreate failed");
  142.    err = FSOpen(fileName, vRefNum, &refNum);
  143.    IfDebug(err, "\pFSOpen failed");
  144.    
  145.    block.spool.spoolProcedure = (long (*)(gxSpoolCommand, struct gxSpoolBlock *)) FileSpoolProc;
  146.    block.reference = refNum;
  147.    block.spool.buffer = nil;
  148.    block.spool.bufferSize = 0;
  149.    GXFlattenShape(source,  gxFontListFlatten | gxFontGlyphsFlatten, &block.spool);
  150.    err = FSClose(refNum);
  151.    IfDebug(err, "\pFSClose failed");
  152. }
  153.  
  154. gxShape FileToShape(Str255 fileName, short vRefNum, long count, const gxViewPort portList[])
  155. {
  156.    userSpool block;
  157.    short refNum;
  158.    short err;
  159.    gxShape dest;
  160.    
  161.    err = FSOpen(fileName, vRefNum, &refNum);
  162.    IfDebug(err, "\pFSOpen failed");
  163.    block.spool.spoolProcedure = (long (*)(gxSpoolCommand, struct gxSpoolBlock *)) FileSpoolProc;
  164.    block.reference = refNum;
  165.    block.spool.buffer = nil;
  166.    block.spool.bufferSize = 0;
  167.    dest = GXUnflattenShape(&block.spool, count, portList);
  168.    err = FSClose(refNum);
  169.    IfDebug(err, "\pFSClose failed");
  170.    return dest;
  171. }
  172.  
  173. Handle FontToHandle(gxFont fontID, long glyphBits[])
  174. {
  175.    userSpool block;
  176.    scalerStream stream;
  177.    
  178.    block.spool.spoolProcedure = (long (*)(gxSpoolCommand, struct gxSpoolBlock *)) HandleSpoolProc;
  179.    block.spool.buffer = nil;
  180.    block.spool.bufferSize = 0;
  181.  
  182.    stream.streamRefCon = fontID;
  183.    stream.types = flattenedStreamType;
  184.    stream.action = downloadStreamAction;
  185.    stream.memorySize = 0;
  186.    stream.variationCount = selectAllVariations;
  187.    stream.variations = nil;
  188.    stream.encoding = 0;
  189.    stream.glyphBits = glyphBits;
  190.    stream.fontName = nil;
  191.  
  192.    GXFlattenFont(fontID, &stream, &block.spool);
  193.    return block.data;
  194. }
  195.  
  196. gxFont HandleToFont(Handle source)
  197. {
  198.    /*
  199.     * GXNewFont does not copy the data in source, so you can't dispose
  200.     * of it until you have called GXDisposeFont().
  201.    */
  202.    return GXNewFont(gxHandleFontStorage, source, 0);
  203. }
  204.  
  205.